package com.qf.petmall.starter.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.petmall.starter.login.entity.Users;
import com.qf.petmall.starter.login.mapper.UsersMapper;
import com.qf.petmall.starter.login.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.petmall.starter.login.shiro.ShiroConstants;
import com.qf.petmall.starter.utils.R;

import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 用户  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-12-05
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Transactional(rollbackFor = Exception.class)

    public boolean regist(Users user) {


        //1. 比对密码是否一致
        final String password = user.getPassword();
        final String repassword = user.getRepassword();
        if ( !(password!=null&&password.equals(repassword)) ){
            // 密码有问题，两次密码不一致

            return false;
        }



        //2. 检查用户名是否存在
//        final User dbUser = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));
        final Users dbUser = this.getOne(new QueryWrapper<Users>().eq("username",user.getUsername()));
        if (dbUser!=null){
            return false;
        }


        //3. 生成salt，对密码进行加密处理
        final String salt = UUID.randomUUID().toString().replaceAll("-", "");
        final Md5Hash md5Hash = new Md5Hash(password, salt, ShiroConstants.HASH_TIMES);
        final String shiroPassword = md5Hash.toHex();

        //4. 保存用户数据到数据库
        user.setSalt(salt);
        user.setPassword(shiroPassword);

        user.setUserRegtime(new Date());
        user.setUserModtime(new Date());

        this.save(user);


        return true;


    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public R upload(MultipartFile userImg,Integer userId) throws IOException {
        if (userImg.isEmpty()) {
            return R.error("上传失败");
        }
        String filename = UUID.randomUUID().toString();
        String originalFilename = userImg.getOriginalFilename();

        String extension = FilenameUtils.getExtension(originalFilename);
        String dir = System.getProperty("user.dir");
        File file = new File(dir + File.separator +"imgs");
        if (!file.exists()) {
            file.mkdirs();
        }
        Users one = this.getOne(new LambdaQueryWrapper<Users>().eq(Users::getUserId,userId));
        if (one != null) {
            String finalFilePath = dir + File.separator +"imgs"+File.separator+ filename + "." + extension;
            userImg.transferTo(new File(finalFilePath));
            one.setUserImg("http://192.168.10.81:8878/users/getimg"+File.separator+filename + "." + extension);
            this.updateById(one);
            return R.ok("上传成功");
        } else {
            return R.error("上传失败");
        }

    }
}
